﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
namespace WebSite.UserCash
{
    public partial class CashFlowList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCashFlowType();
                BindMoney();
                LoadContent(false);
            }
        }

        #region 加载内容
        private void LoadContent(bool mark)
        {
            //设置控件属性，从BasePage中得到
            PageConfig(CashFlowUPager);

            //List<Member> list = new List<Member();
            CashFlowHelper cfHelper = new CashFlowHelper();
            Criteria c = new Criteria();
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();
            string type = ddlType.SelectedValue;

            Users uinfo = UserBase.GetCurrentUser();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);

            if (!string.IsNullOrEmpty(begindate))
            {
                try
                {
                    DateTime from = Convert.ToDateTime(begindate);
                    // 直接输入日期类型
                    c.Add(CriteriaType.MoreThanEquals, "CreateDate", from);
                }
                catch (Exception ex)
                {
                    Tools.Alert("日期格式不正确");
                    throw ex;
                }
                
            }
            if (!string.IsNullOrEmpty(enddate))
            {
                try
                {
                    DateTime end = Convert.ToDateTime(enddate);
                    // 直接输入日期类型
                    c.Add(CriteriaType.LessThanEquals, "CreateDate", end);
                }
                catch (Exception ex)
                {
                    Tools.Alert("日期格式不正确");
                    throw ex;
                }
            }
            if (type != "-1")
            {
                c.Add(CriteriaType.Equals, "Type", type);
            }

            CashFlowUPager.ItemCount = cfHelper.QueryCashFlowCountBySearch(c);

            // 以下的值从BasePage中得到
            CashFlowUPager.UrlFormat = UrlFormat;
            CashFlowUPager.PrefixText = GetPreFixText(CashFlowUPager);

            if (mark)
            {
                PagerInit(CashFlowUPager);
            }

            string[] fields = new string[] { "ID", "UsersID", "Money", "Description","Type", "CreateDate" };

            DataTable dt = new DataTable();
            dt = cfHelper.QueryCashFlowBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, CashFlowUPager.Begin - 1, CashFlowUPager.Count, fields);

            dt.Columns.Add(new DataColumn("strType"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string gettype = dr["Type"] + "";
                    //查找类别对应名称
                    CashFlowType cftinfo = new CashFlowType();
                    CashFlowTypeHelper cfthelper = new CashFlowTypeHelper();
                    cftinfo = cfthelper.GetCashFlowType(gettype);

                    dr["strType"] = cftinfo.TypeName;
                }
            }

            rptCashFlowList.DataSource = dt;
            rptCashFlowList.DataBind();
        }
        #endregion

        #region 查询
        protected void ibSearch_Click(object sender, ImageClickEventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();
            string type = ddlType.SelectedValue;

            //将查询条件放入Session中
            Session["QueryList"] = begindate + "@" + enddate + "@" + type + "@" + "CashFlowList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 4)
            {
                string page = conditionAll[3];
                if (page == "CashFlowList")
                {
                    txtBeginDate.Text = conditionAll[0];
                    txtEndDate.Text = conditionAll[1];
                    ddlType.SelectedValue = conditionAll[2];
                }
            }
        }
        #endregion

        #region 加载账户信息
        private void BindMoney()
        {
            Users uinfo = UserBase.GetCurrentUser();
            UsersHelper uhelper = new UsersHelper();
            //账户总收入
            float totalmonty = uhelper.GetTotalMoney(uinfo.ID);
            litTotalMoney.Text = totalmonty.ToString();
            //账户目前金额
            float currentmoney = uhelper.GetCurrentMoney(uinfo.ID);
            lblCurrentMoney.Text = currentmoney.ToString();
            //账户可提现金额
            float cashmoney = uhelper.GetCashMoney(uinfo.ID);
            lblCashMoney.Text = cashmoney.ToString();
        }
        #endregion

        #region 加载现金流类别
        private void BindCashFlowType()
        {
            CashFlowTypeHelper cfthelper = new CashFlowTypeHelper();
            List<CashFlowType> cftlist = cfthelper.QueryCashFlowTypeByAll(new Order[] { new Order("OrderID", OrderMode.Asc) }, 0, 0, null);
            ddlType.DataSource = cftlist;
            ddlType.DataTextField = "TypeName";
            ddlType.DataValueField = "ID";
            ddlType.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlType.Items.Insert(0, li);
        }
        #endregion 
    }
}